﻿/**************************************************
 *  Author :    youping
 *  CreateDate: 2020-10-11
 *  Title:      文件重命名
**************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Demo.FolderOp
{
    public partial class FileRename : Form
    {
        public FileRename()
        {
            InitializeComponent();
        }

        private void btnFBD_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                //MessageBox.Show(folderBrowserDialog.SelectedPath);
                this.txtFolderUrl.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void txtGetNames_Click(object sender, EventArgs e)
        {
            string currentPath = this.txtFolderUrl.Text.Trim();
            string result = CommonMethod.IsPathUrlRight(currentPath);
            if (!string.IsNullOrWhiteSpace(result))
            {
                MessageBox.Show(result);
                return;
            }
            //第二种方法
            DirectoryInfo folder = new DirectoryInfo(currentPath);


            this.listBox1.Items.Clear();
            foreach (FileInfo myfile in folder.GetFiles())
            {
                this.listBox1.Items.Add(myfile.ToString());
            }
        }

        //确认将old的件替换成新名字
        private void btnReplaceWithShow_Click(object sender, EventArgs e)
        {
            //清空listbox2数据
            this.listBox2.Items.Clear();
            if (string.IsNullOrWhiteSpace(this.txtSearchContent.Text))
            {
                foreach (string item in this.listBox1.Items)
                {
                    this.listBox2.Items.Add(item);
                }
            }
            else
            {
                foreach (string item in this.listBox1.Items)
                {
                    this.listBox2.Items.Add(item.Replace(this.txtSearchContent.Text, this.txtReplaceWith.Text));
                }
            }
        }

        //同步写到文件中
        private void btnReplaceWithReal_Click(object sender, EventArgs e)
        {
            string currentPath = this.txtFolderUrl.Text.Trim();
            string result = CommonMethod.IsPathUrlRight(currentPath);
            if (!string.IsNullOrWhiteSpace(result))
            {
                MessageBox.Show(result);
                return;
            }

            if (this.listBox1.Items.Count != this.listBox2.Items.Count)
            {
                MessageBox.Show("请先点击确认显示，再进行转换操作");
                return;
            }


            DirectoryInfo folder = new DirectoryInfo(currentPath);
            FileInfo[] myFileInfos = folder.GetFiles();
            if (myFileInfos.Count() != this.listBox1.Items.Count)
            {
                MessageBox.Show("请按照顺序操作");
                return;
            }

            int index = 0;
            foreach (FileInfo myfile in myFileInfos)
            {
                if (this.listBox1.Items[index].ToString() == myfile.ToString() && this.listBox1.Items[index].ToString() != this.listBox2.Items[index].ToString())
                {
                    ////转换LastWriteTime的格式为“yyyy.mm.dd”。
                    //string LastWriteTime = string.Format("{0:yyyyMMddHHmmss}", myfile.LastWriteTime);
                    ////在转换格式后的LastWriteTime两边加上“【】”。
                    //string fileNamePre = "【" + LastWriteTime + "】";
                    string fileNamePre = string.Empty;
                    //修改文件名的方法。
                    var arr = this.listBox2.Items[index].ToString().Split('.');
                    myfile.MoveTo(currentPath + "\\" + arr[0] + fileNamePre + "." + arr[1]);
                }
                index++;
            }
            MessageBox.Show("保存成功");
        }

        //字符转换成对应的内容
        private void btnTrans_Click(object sender, EventArgs e)
        {
            int currentIndex = -1;
            int loop = 0;
            string temp = string.Empty;

            if (this.listBox1.Items.Count != this.listBox2.Items.Count)
            {
                MessageBox.Show("请先点击确认显示，再进行转换操作");
                return;
            }

            foreach (string str in this.listBox1.Items)
            {
                currentIndex = str.IndexOf("20");
                if (currentIndex > -1 && str.Length >= (currentIndex + 4))
                {

                    temp = Enum.GetName(typeof(SystemMenu.PatentType), Convert.ToInt32(str.Substring(currentIndex + 4, 1)));
                    if (this.listBox2.Items[loop].ToString().IndexOf(temp) < 0)
                    {
                        this.listBox2.Items[loop] = temp + "_" + this.listBox2.Items[loop].ToString();
                    }
                }
                loop++;
            }
        }
    }
}
